package com.weiquan.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiquan.annotation.ApiLog;
import com.weiquan.common.Result;
import com.weiquan.entity.ApiLogRecord;
import com.weiquan.service.ApiLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * API日志管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/api-logs")
@Tag(name = "API日志管理", description = "API请求日志的查询和管理")
public class ApiLogController {

    @Autowired
    private ApiLogService apiLogService;

    /**
     * 分页查询API日志
     */
    @ApiLog(value = "查询API日志")
    @GetMapping("/page")
    @Operation(summary = "分页查询API日志", description = "分页查询API请求日志记录")
    public Result<Page<ApiLogRecord>> getApiLogPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int pageSize,
            @Parameter(description = "请求方法") @RequestParam(required = false) String method,
            @Parameter(description = "状态") @RequestParam(required = false) String status,
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        
        Page<ApiLogRecord> page = apiLogService.getApiLogPage(pageNum, pageSize, method, status, startTime, endTime);
        return Result.success(page);
    }

    /**
     * 获取API日志详情
     */
    @ApiLog(value = "获取API日志详情")
    @GetMapping("/{id}")
    @Operation(summary = "获取API日志详情", description = "根据ID获取API日志的详细信息")
    public Result<ApiLogRecord> getApiLogDetail(
            @Parameter(description = "日志ID") @PathVariable Long id) {
        
        ApiLogRecord apiLog = apiLogService.getApiLogById(id);
        return Result.success(apiLog);
    }

    /**
     * 获取API统计信息
     */
    @ApiLog(value = "获取API统计信息")
    @GetMapping("/statistics")
    @Operation(summary = "获取API统计信息", description = "获取API调用的统计数据")
    public Result<Object> getApiStatistics() {
        
        Object statistics = apiLogService.getApiStatistics();
        return Result.success(statistics);
    }

    /**
     * 清理过期日志
     */
    @ApiLog(value = "清理过期日志")
    @DeleteMapping("/cleanup")
    @Operation(summary = "清理过期日志", description = "删除30天前的API日志记录")
    public Result<Void> cleanupExpiredLogs() {
        
        apiLogService.deleteExpiredLogs();
        return Result.success();
    }

    /**
     * 导出API日志
     */
    @ApiLog(value = "导出API日志")
    @GetMapping("/export")
    @Operation(summary = "导出API日志", description = "导出指定条件的API日志为Excel文件")
    public Result<String> exportApiLogs(
            @Parameter(description = "请求方法") @RequestParam(required = false) String method,
            @Parameter(description = "状态") @RequestParam(required = false) String status,
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        
        // 这里可以实现Excel导出逻辑
        // 暂时返回成功消息
        log.info("导出API日志请求，条件：method={}, status={}, startTime={}, endTime={}", 
                method, status, startTime, endTime);
        
        return Result.success("导出任务已提交，请稍后下载");
    }

    /**
     * 获取错误日志统计
     */
    @ApiLog(value = "获取错误日志统计")
    @GetMapping("/error-statistics")
    @Operation(summary = "获取错误日志统计", description = "获取API错误日志的统计信息")
    public Result<Object> getErrorStatistics(
            @Parameter(description = "统计天数") @RequestParam(defaultValue = "7") int days) {
        
        // 这里可以实现错误统计逻辑
        log.info("获取{}天内的错误日志统计", days);
        
        return Result.success("错误统计数据");
    }

    /**
     * 获取慢接口统计
     */
    @ApiLog(value = "获取慢接口统计")
    @GetMapping("/slow-api-statistics")
    @Operation(summary = "获取慢接口统计", description = "获取响应时间较长的接口统计")
    public Result<Object> getSlowApiStatistics(
            @Parameter(description = "响应时间阈值(ms)") @RequestParam(defaultValue = "1000") long threshold,
            @Parameter(description = "统计天数") @RequestParam(defaultValue = "7") int days) {
        
        // 这里可以实现慢接口统计逻辑
        log.info("获取{}天内响应时间超过{}ms的接口统计", days, threshold);
        
        return Result.success("慢接口统计数据");
    }
}